//
//  ScroDeleteTableView.m
//  Koina
//
//  Created by welldone on 14-9-28.
//  Copyright (c) 2014年 net.koina. All rights reserved.
//

#import "ScroDeleteTableView.h"
#import "config.h"
#import "ScroDeleteCell.h"
#import "NoTouchView.h"


@interface ScroDeleteTableView ()
@property (nonatomic,strong)NoTouchView *helpView;
@property (nonatomic,strong)ScroDeleteCell *bufSeletedCell;


-(void)hideDataDeletaBtn;

-(void)btnOnClik:(UIButton*)sender;

@end

@implementation ScroDeleteTableView
@synthesize helpView,bufSeletedCell,menuStatedelegate;


-(instancetype)init{
    self = [super init];
    if (self) {
        
        [self initView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self initView];
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    
    if (self) {
        [self initView];
    }
    return self;
}





-(void)initView{
    

    helpView = [[NoTouchView alloc]initWithFrame:CGRectMake(0, 0,PMW,self.frame.size.height)];
    [helpView setBackgroundColor:[UIColor clearColor]];
    [helpView addTarget:self action:@selector(btnOnClik:) forControlEvents:UIControlEventTouchDown];
}



-(void)btnOnClik:(UIButton*)sender{
    if (sender==helpView) {
        [self hideDataDeletaBtn];
    }
}


#pragma mark deleteBtn delegate
-(void)menuDidShow:(UITableViewCell*)cell{
    if ([cell isKindOfClass:[ScroDeleteCell class]]) {
        bufSeletedCell = (ScroDeleteCell*)cell;
        
        [self addSubview:helpView];
        
        if (bufSeletedCell.showFuntionBtn1) {
            CGRect buf = [bufSeletedCell convertRect:bufSeletedCell.funtionBtn1.frame toView:helpView];
            [helpView.hotRects addObject:[NSValue valueWithCGRect:buf]];
        }
        
        
        if (bufSeletedCell.showFuntionBtn2) {
            CGRect buf = [bufSeletedCell convertRect:bufSeletedCell.funtionBtn2.frame toView:helpView];
            [helpView.hotRects addObject:[NSValue valueWithCGRect:buf]];
        }
        
        if (bufSeletedCell.showFuntionBtn3) {
            CGRect buf = [bufSeletedCell convertRect:bufSeletedCell.funtionBtn3.frame toView:helpView];
            [helpView.hotRects addObject:[NSValue valueWithCGRect:buf]];
        }
        
        if ([self.menuStatedelegate respondsToSelector:@selector(cellMenDidShow:cell:)]) {
            [self.menuStatedelegate cellMenDidShow:self cell:(ScroDeleteCell*)cell];
        }
    }
}


-(void)MenuDidHide:(UITableViewCell*)cell{
    [helpView removeFromSuperview];
    [helpView.hotRects removeAllObjects];
    bufSeletedCell = nil;
    
    if ([cell isKindOfClass:[ScroDeleteCell class]]) {
        
        if ([self.menuStatedelegate respondsToSelector:@selector(cellMenDidHide:cell:)]) {
            [self.menuStatedelegate cellMenDidHide:self cell:(ScroDeleteCell*)cell];
        }
    }
    
}




#pragma mark scrollView delegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self hideDataDeletaBtn];
}



-(void)hideDataDeletaBtn{
    if (bufSeletedCell) {
        [bufSeletedCell cloadMenu];
        [helpView removeFromSuperview];
    }
}



@end
